package com.mc.servlet;

import com.mc.bean.Student;
import com.mc.service.StudentService;
import com.mc.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zh
 * 修改学生
 */
@WebServlet("/studentedit.action")
public class StudentEditServite extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑对象
        StudentService studentService=new StudentServiceImpl();
        //接收编号
        String sidstr=req.getParameter("sid");
        //更改为int类型
        int sid=Integer.parseInt(sidstr);
        //接收type
        String type = req.getParameter("type");
        Student student=studentService.findBySid(sid);
        if(type.equals("1")){

            //将对象放入作用域中
            req.setAttribute("student",student);
            //转发到修改页面
            req.getRequestDispatcher("editStudent.jsp").forward(req,resp);
        }else if(type.equals("2")){
            //接收
           String sname = req.getParameter("sname");
            String stel=req.getParameter("stel");
            String sageStr = req.getParameter("sage");
            String  ssex=req.getParameter("ssex");
            System.out.println(ssex);
            String sheadurl=req.getParameter("sheadursl");
            int colid=Integer.parseInt(req.getParameter("colid"));
            int gid=Integer.parseInt(req.getParameter("gid"));
            int sage = Integer.parseInt(sageStr);
            System.out.println(sname);
            System.out.println(stel);
            System.out.println(sage);
            System.out.println(ssex);
            System.out.println(sheadurl);
            System.out.println(colid);
            System.out.println(gid);

            //通过业务逻辑对象调用修改部门的方法。
            student.setSname(sname);
            student.setStel(stel);
            student.setSage(sage);
            student.setSsex(ssex);
            student.setSheadurl(sheadurl);
            student.setGid(gid);
            student.setColid(colid);
            System.out.println(student);
            boolean flag = studentService.update(student);
            System.out.println(student);
            System.out.println(flag);
            //重定向
            resp.sendRedirect("studentlist.action");

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
